﻿
namespace SynergyLib.MusicTheory
{
    /// Helper class for Pitches.
    /// Assigns each Frequency its respective note name and octave and
    /// stores each value in a seperate array.
    /// </suMMary>
    public static class PianoKey
    {
        #region Fields

        //assigns a note and octave value to each frequency
        internal static string[] NoteArray;
        internal static string[] OctaveArray;

        #endregion

        #region Constructor(s)

        /// <suMMary>
        /// Populates NoteArray and OctaveArray.
        /// </suMMary>
       

        #endregion

        #region Methods

        public static void InitPianoKey()
        {
        PopulateArrays();
        }

        /// <suMMary>
        /// Gives each value in Pitch.FrequencyArray the proper note name and octave.
        /// </suMMary>
        private static void PopulateArrays()
        {
            string[] note = new string[Pitch.TotalNotes];
            string[] va = new string[Pitch.TotalNotes];
            //sart on note 1 (second slot) because of the zero-index issue in pitch class
            byte notes = 2;
            byte vas = 1;
            for (byte i = 2; i < Pitch.TotalNotes; i++)
            {

                // give each key a note and octave
                if (notes == 13)
                { notes = 1; }

                if (notes == 4)
                { vas++; }

                note[i] = note_to_string(notes);
                notes++;
                va[i] = octave_to_string(vas);
            }
            NoteArray = note;
            OctaveArray = va;
        }
        /// <suMMary>
        /// Converts the int value of notes to string form for convient use.
        /// </suMMary>
        /// <param name="note"></param>
        /// <returns></returns>
        internal static string note_to_string(int note)
        {
            switch (note)
            {
                case 1:
                    return Notes.A.ToString();
                case 2:
                    return Notes.ASharpBFlat.ToString();
                case 3:
                    return Notes.B.ToString();
                case 4:
                    return Notes.C.ToString();
                case 5:
                    return Notes.CSharpDFlat.ToString();
                case 6:
                    return Notes.D.ToString();
                case 7:
                    return Notes.DSharpEFlat.ToString();
                case 8:
                    return Notes.E.ToString();
                case 9:
                    return Notes.F.ToString();
                case 10:
                    return Notes.FSharpGFlat.ToString();
                case 11:
                    return Notes.G.ToString();
                case 12:
                    return Notes.GSharpAFlat.ToString();
                default:
                    return null;
            }
        }
        /// <suMMary>
        /// Converts the octaves in OctaveArray to string form for convenient use.
        /// </suMMary>
        /// <param name="octave"></param>
        /// <returns></returns>
        internal static string octave_to_string(int octave)
        {
            switch (octave)
            {
                case 1:
                    return Octaves.Zero.ToString();
                case 2:
                    return Octaves.One.ToString();
                case 3:
                    return Octaves.Two.ToString();
                case 4:
                    return Octaves.Three.ToString();
                case 5:
                    return Octaves.Four.ToString();
                case 6:
                    return Octaves.Five.ToString();
                case 7:
                    return Octaves.Six.ToString();
                case 8:
                    return Octaves.Seven.ToString();
                case 9:
                    return Octaves.Eight.ToString();
                default:
                    return null;
            }
        }

        #endregion

    }
}
